<?php

namespace app\admin\controller\agreement;

use app\common\controller\Backend;

/**
 * 协议列管理
 *
 * @icon fa fa-circle-o
 */
class Agreement extends Backend
{

    /**
     * Agreement模型对象
     * @var \app\admin\model\agreement\Agreement
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\agreement\Agreement;

    }


    public function del($ids = "")
    {
        if ($ids) {
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            $list = $this->model->where($pk, 'in', $ids)->select();
            //删除前，行为钩子
            if (false === $this->_callback('_delete_filter', $list)) {
                $this->error(__('delete fail!'));
            }
            $count = 0;
            foreach ($list as $k => $v) {
                $count += $v->where(['id'=>$v['id']])->update(['delete_time'=>time()]);
            }
            if ($count) {
                //删除后，行为钩子
                if (false !== $this->_callback('_delete_result', $result)) {
                    $this->success();
                }else{
                    $this->error(__('delete fail!'));
                }
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 当前对象回调成员方法
     * @param string $method
     * @param array|bool $data
     * @return bool
     */
    protected function _callback($method, &$data)
    {
        foreach ([$method, "_" . $this->request->action() . "{$method}"] as $_method) {
            if (method_exists($this, $_method) && false === $this->$_method($data)) {
                return false;
            }
        }
        return true;
    }


}
